package com.tyfspring.beans.config;

import java.util.Objects;

import static com.tyfspring.util.Constants.SCOPE_PROTOTYPE;
import static com.tyfspring.util.Constants.SCOPE_SINGLETON;

/**
 * @Description 保存bean的信息，包括class类型，方法构造函数，bean属性，bean的scope等
 * @Author shallow
 * @Date 2023/3/26 13:00
 */

public class BeanDefinition {

    //class文件
    private Class beanClass;
    //该类的属性列表
    private PropertyValues propertyValues;
    //初始化方法
    private String initMethodName;
    //销毁方法
    private String destroyMethodName;
    //作用域
    private String scope = SCOPE_SINGLETON;
    //是否为单例
    private boolean singleton = true;
    //是否为原型
    private boolean prototype = false;
    //是否懒加载
    private boolean lazyInit=false;

    public BeanDefinition(Class beanClass) {
        this.beanClass = beanClass;
        this.propertyValues = new PropertyValues();
    }

    public BeanDefinition(Class beanClass, PropertyValues propertyValues) {
        this.beanClass = beanClass;
        this.propertyValues = propertyValues;
    }

    public void setScope(String scope){
        this.scope=scope;
        this.singleton = SCOPE_SINGLETON.equals(scope);
        this.prototype = SCOPE_PROTOTYPE.equals(scope);
    }

    public boolean isSingleton() {
        return this.singleton;
    }

    public boolean isPrototype() {
        return this.prototype;
    }

    public Class getBeanClass() {
        return beanClass;
    }

    public void setBeanClass(Class beanClass) {
        this.beanClass = beanClass;
    }

    public PropertyValues getPropertyValues() {
        return propertyValues;
    }

    public void setPropertyValues(PropertyValues propertyValues) {
        this.propertyValues = propertyValues;
    }

    public String getInitMethodName() {
        return initMethodName;
    }

    public void setInitMethodName(String initMethodName) {
        this.initMethodName = initMethodName;
    }

    public String getDestroyMethodName() {
        return destroyMethodName;
    }

    public void setDestroyMethodName(String destroyMethodName) {
        this.destroyMethodName = destroyMethodName;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        BeanDefinition that = (BeanDefinition) o;
        return getBeanClass().equals(that.getBeanClass());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getBeanClass());
    }

    public void setLazyInit(boolean b){
        lazyInit=b;
    }

    public boolean isLazyInit(){
        return lazyInit;
    }
}
